$(function(){
	$.ajax({
		url:'allbrand',
		dataType:'json',
		success:function(data){
			//alert(data.commentTop10);
			//查询所有手机品牌然后放在导航栏
			var navbar="";
			var $leftBrand=$('.leftnavbar ul li');
			var $rightdiv=$('.rightdiv');
			var $leftdiv=$('.leftdiv');
			var $centerdiv=$('.centerdiv');
			$.each(data.allBrand,function(i,v){
				//主页标题
				navbar+='<li><a href="" target="_blank">'+v.brandName+'</a></li>'
				//左边导航栏
				$leftBrand.eq(i).prepend('<a href="">'+v.brandName+'</a>');
				$.each(v.phones,function(id,phone){
					var phonediv='<div><div class="xuangou_left fl"><a href=""><div class="img fl"><img src="./image/xm6_80.png" alt=""></div><span class="fl">'+phone.name+'</span><div class="clear"></div></a></div><div class="clear"></div></div>';
					//前6个手机放左边，6-11放中间，12-17放右边
					if(id<6){
						$leftdiv.eq(i).append(phonediv);
					}else if(id>5&&id<12){
						$centerdiv.eq(i).append(phonediv);
					}else if(id>11&&id<18){
						$rightdiv.eq(i).append(phonediv);
					}else if(id>17){
						//跳出循环
						return false;
					}
				});
			});
			//明星单品
			var hotPhone="";
			$.each(data.salesTop5,function(i,v){
				hotPhone+='<div class="mingxing fl"><div class="sub_mingxing"><a href=""><img src="./image/pinpai1.png" alt=""></a></div><div class="pinpai"><a href="">'+v.name+'</a></div><div class="youhui"></div><div class="jiage">'+v.zuidijia+'元</div></div>';
			});
			//热评精品
			//第一行
			var welcomePhone_1="";
			//第二行
			var welcomePhone_2="";
			var $content=$('.content');
			$.each(data.commentTop10,function(i,v){
				var tempdata='<div class="remen fl"><div class="xinpin"><span style="background:#fff"></span></div><div class="tu"><a href=""><img src="./image/xmad_14926862610682_UhkfS.png"></a></div><div class="miaoshu"><a href="">'+v.name+'</a></div><div class="jiage">'+v.zuidijia+'</div><div class="pingjia">'+v.pinglunshu+'人评价</div></div>';
				if(i<5){
					welcomePhone_1+=tempdata;
				}else{
					welcomePhone_2+=tempdata;
				}
			});
			//添加明星单品
			$('.hot_phone').prepend(hotPhone);
			//添加左边的导航栏
			$('.navbar ul').append(navbar);
			//添加热评精品
			$content.eq(0).prepend(welcomePhone_1);
			$content.eq(1).prepend(welcomePhone_2);
			//数据加载完成后，设置轮播div的位置和宽高
			var $leftnavbar=$('.leftnavbar');
			var offset=$leftnavbar.offset();
			var top = offset.top;
			var left= offset.left;
			var leftwidth=$leftnavbar.width();
			//轮播图的设置
			layui.use('carousel', function(){
				  var carousel = layui.carousel;
				  carousel.render({
				    elem: '#maximgdiv'
				    ,width: 1226-leftwidth+'px' //设置容器宽度
				    ,height:'460px' ////设置容器高度
				    ,arrow: 'always' //始终显示箭头
				    ,autoplay:true //自动播放
				  });
				});
			$('#maximgdiv').offset({'top':top,'left':left+leftwidth});
		}
	});
});